﻿/*
 * Created by SharpDevelop.
 * User: MyWife
 * Date: 6/22/2010
 * Time: 10:32 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using popLib;

namespace FileAssociateManager
{
	/// <summary>
	/// Description of Form1.
	/// </summary>
	public partial class Form1 : Form
	{
		
		
		public Form1()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			listBox1.MouseDown += listbox_mouseDown;
			listBox2.MouseDown += listbox_mouseDown;
			listBox3.MouseDown += listbox_mouseDown;
			listBox4.MouseDown += listbox_mouseDown;
			listBox5.MouseDown += listbox_mouseDown;
		}
		
		private void listbox_mouseDown(object sender, System.Windows.Forms.MouseEventArgs e){
			ListBox list = (ListBox)sender;
			int indexOfItem = list.IndexFromPoint(e.X, e.Y);
			if( indexOfItem >= 0 )
			{				
				ReturnDragObject obj = new ReturnDragObject();
				obj.ReturnControl	 = list;
				obj.SelectedText	 = list.SelectedItem.ToString();
				obj.SelectedValue	 = list.SelectedItem.ToString();
				list.DoDragDrop( obj , DragDropEffects.Copy );					
			}
		}
		
		
		private void treeProject_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
		{
			
			e.Effect = DragDropEffects.None;			
			
			Point pt = new Point(e.X, e.Y);           
			pt = treeProject.PointToClient(pt);
			TreeNode node = treeProject.GetNodeAt(pt);
			if (node != null)
			{
				ReturnDragObject obj  = (ReturnDragObject)e.Data.GetData(typeof(ReturnDragObject));
				ListBox list		  = (ListBox)obj.ReturnControl;
				int imgIndex		  = Convert.ToInt16( list.Tag );
				
				
				if( Convert.ToInt16( node.Tag ) == imgIndex - 1 ){
					e.Effect = DragDropEffects.Copy;
					treeProject.SelectedNode = node;
				}
			}

		}

		private void treeProject_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
		{
			Point pt		= new Point(e.X, e.Y);
			pt				= treeProject.PointToClient(pt);           
			TreeNode node	= treeProject.GetNodeAt(pt);
			if (node != null)
			{
			
				ReturnDragObject obj  = (ReturnDragObject)e.Data.GetData(typeof(ReturnDragObject));
				ListBox list		  = (ListBox)obj.ReturnControl;
				TreeNode addNode	  = new TreeNode( obj.SelectedText, Convert.ToInt16( list.Tag ), Convert.ToInt16( list.Tag ));
				addNode.Tag   		  = list.Tag;

				//add node to tree
				node.Nodes.Add( addNode );
				node.Expand();

				
			}
		}
		
		void TreeProjectDragEnter(object sender, DragEventArgs e)
		{
			if(e.Data.GetDataPresent(DataFormats.Text))
				e.Effect = DragDropEffects.All;
			else
				e.Effect = DragDropEffects.None;
		}
	}
	
	public class ReturnDragObject
	{
		object returnControl;
		string selectedText;
		string selectedValue;

		public object ReturnControl{
			get{return returnControl;}
			set{returnControl = value;}
		}
		public string SelectedText
		{
			get{return selectedText;}
			set{selectedText = value;}
		}
		public string SelectedValue
		{
			get{return selectedValue;}
			set{selectedValue = value;}
		}
	}
}
